Page({
  data: {
    // 搜索关键词
    searchKeyword: '',
    // 当前用户信息
    currentUser: {
      name: '张同学',
      avatar: '/static/avatar.png'
    },
    // 消息数量
    messageCount: 5,
    // 轮播图数据
    banners: [
      {
        id: 1,
        title: '新学期教材特惠',
        description: '各类教材低至3折，毕业学长学姐良心推荐',
        buttonText: '立即查看',
        image: '/static/banner1.jpg'
      },
      {
        id: 2,
        title: '电子产品专场',
        description: '手机、电脑、平板，学生专属优惠',
        buttonText: '立即查看',
        image: '/static/banner2.jpg'
      },
      {
        id: 3,
        title: '毕业季清仓活动',
        description: '毕业同学闲置物品，低价转让',
        buttonText: '立即查看',
        image: '/static/banner3.jpg'
      }
    ],
    // 分类数据
    categories: [
      { id: 1, name: '教材书籍', icon: 'book' },
      { id: 2, name: '电子产品', icon: 'laptop' },
      { id: 3, name: '生活用品', icon: 'home' },
      { id: 4, name: '运动器材', icon: 'dumbbell' },
      { id: 5, name: '服装鞋帽', icon: 'tshirt' }
    ],
    // 推荐商品
    recommendedProducts: [
      {
        id: 1,
        name: '高数第七版教材',
        price: 25,
        image: '/static/product1.jpg',
        location: '东校区',
        likes: 18
      },
      {
        id: 2,
        name: '9成新iPad 2020',
        price: 2000,
        image: '/static/product2.jpg',
        location: '西校区',
        likes: 32
      },
      {
        id: 3,
        name: '宿舍收纳神器',
        price: 45,
        image: '/static/product3.jpg',
        location: '南校区',
        likes: 24
      },
      {
        id: 4,
        name: '网球拍一对',
        price: 80,
        image: '/static/product4.jpg',
        location: '北校区',
        likes: 15
      }
    ],
    // 拍卖商品
    auctions: [
      {
        id: 1,
        name: '限量版球鞋 AJ1',
        currentPrice: 850,
        image: '/static/auction1.jpg',
        progress: 75,
        remainingTime: '2小时12分'
      },
      {
        id: 2,
        name: 'Switch游戏机 + 健身环',
        currentPrice: 1200,
        image: '/static/auction2.jpg',
        progress: 60,
        remainingTime: '5小时30分'
      }
    ]
  },

  // 生命周期
  onLoad() {
    // 页面加载时执行
    this.checkLoginStatus();
  },

  onShow() {
    // 更新自定义tabBar选中状态
    this.getTabBar().setData({
      selected: 0
    });
  },

  // 检查登录状态
  checkLoginStatus() {
    const isAuthenticated = wx.getStorageSync('isAuthenticated') || false;
    if (!isAuthenticated) {
      wx.redirectTo({
        url: '/pages/login/login'
      });
    }
  },

  // 搜索输入事件
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  // 执行搜索
  doSearch() {
    wx.showToast({
      title: '搜索: ' + this.data.searchKeyword,
      icon: 'none'
    });
  },

  // 跳转到商品发布页
  goToPublish() {
    wx.navigateTo({
      url: '/pages/Product/product'
    });
  },

  // 查看分类
  viewCategory(e) {
    const categoryId = e.currentTarget.dataset.id;
    wx.showToast({
      title: '查看分类: ' + categoryId,
      icon: 'none'
    });
  },

  // 查看商品详情
  viewProduct(e) {
    const productId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/Product/detail?id=' + productId
    });
  },

  // 查看拍卖详情
  viewAuction(e) {
    const auctionId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/auction/detail?id=' + auctionId
    });
  },

  // 立即出价
  placeBid(e) {
    const auctionId = e.currentTarget.dataset.id;
    // 阻止事件冒泡，防止触发viewAuction
    e.stopPropagation();
    wx.showModal({
      title: '参与竞拍',
      content: '确认参与该商品的竞拍吗？',
      success(res) {
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/Transaction/transaction?id=' + auctionId + '&type=auction'
          });
        }
      }
    });
  },

  // 查看消息
  viewMessages() {
    wx.switchTab({
      url: '/pages/Message/message'
    });
  }
}) 